dim(GRAND_KNOW_lftdcat)
table(GRAND_KNOW_lftdcat[,12])
sum(table(GRAND_KNOW_lftdcat[,12]))

NR=subset(GRAND_KNOW_lftdcat,GRAND_KNOW_lftdcat[,12]>4)
dim(NR)
NR_15_24=NR[NR[,15]==2,];dim(NR_15_24) #  595
NR_25_OL=NR[NR[,15]!=2,];dim(NR_25_OL) # 5125

table(NR_15_24[,12]);sum(table(NR_15_24[,12]));mean(NR_15_24[,12]);sd(NR_15_24[,12])
table(NR_25_OL[,12]);sum(table(NR_25_OL[,12]));mean(NR_25_OL[,12]);sd(NR_25_OL[,12])

# 15_24: CD= .28, n=595, nmin=5125, CI=( .22, .34)

##################################################

par(mfrow=c(1,2))

stap = 0.01
ncel = 100
nresp = nrow(GRAND_KNOW_lftdcat)
nsurv = 7

uit_15_24_survey = matrix(0,ncel,nsurv)
min_15_24_survey = matrix(0,ncel,nsurv)

for (m in 1:nsurv)
{
  n_15_24 = 0
  nmin_15_24 = 0
  for (i in 1:nresp)
  {
    if (!is.na(GRAND_KNOW_lftdcat[i,m+7])&GRAND_KNOW_lftdcat[i,m+7]>4)
    {
      db = dbinom(GRAND_KNOW_lftdcat[i,m],GRAND_KNOW_lftdcat[i,m+7],seq(0.005,0.995,0.01))
      uiti = matrix(db,ncel,1)/(sum(db)*stap)
      if (GRAND_KNOW_lftdcat[i,15]==2) { uit_15_24_survey[,m] = uit_15_24_survey[,m] + uiti ; n_15_24 = n_15_24 + 1 }
      if (GRAND_KNOW_lftdcat[i,15]!=2) { min_15_24_survey[,m] = min_15_24_survey[,m] + uiti ; nmin_15_24 = nmin_15_24 + 1 }
    }
  }
  uit_15_24_survey[,m] = uit_15_24_survey[,m] / n_15_24;print(n_15_24)
  min_15_24_survey[,m] = min_15_24_survey[,m] / nmin_15_24;print(nmin_15_24)
}

p=seq(0.005,0.995,stap)
plot(p,min_15_24_survey[,5],col="red",main='Dont Know Age PO',type='l',ylim=c(0,8),axes=FALSE,lwd=2,
xlab='Probability of Dont Know',ylab='Plausibility')
axis(side=1,at=seq(0,1,0.1));axis(side=2,at=seq(0,8,2),las=1)
lines(p,uit_15_24_survey[,5],col="blue",lwd=2)
legend('topright',bty='n',c('15-24 years','Older than 24 years'),col=c('blue','red'),lty=c(1,1))

y1=uit_15_24_survey[,5];y2=min_15_24_survey[,5]
DIF=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
dif=(x-1)*stap-(y-1)*stap
DIF[x,y]=dif
}}
DIF=sign(DIF)
DEN=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
den=y1[x]*y2[y]*stap^2
DEN[x,y]=den
}}
CD_real=sum(DIF*DEN)/sum(DEN);CD_real



#####################################

dim(GRAND_NEUT_oplcat)
table(GRAND_NEUT_oplcat[,8])
sum(table(GRAND_NEUT_oplcat[,8]))

NR=subset(GRAND_NEUT_oplcat,GRAND_NEUT_oplcat[,8]>4)
dim(NR)
NR_WO=NR[NR[,11]==6,];dim(NR_WO) #  434
NR_OT=NR[NR[,11]!=6,];dim(NR_OT) # 4723

table(NR_WO[,8]);sum(table(NR_WO[,8]));mean(NR_WO[,8]);sd(NR_WO[,8])
table(NR_OT[,8]);sum(table(NR_OT[,8]));mean(NR_OT[,8]);sd(NR_OT[,8])

# WO   : CD=-.16, n=434, nmin=4723, CI=(-.23,-.09)

##################################################

stap = 0.01
ncel = 100
nresp = nrow(GRAND_NEUT_oplcat)
nsurv = 5

uit_wo_survey = matrix(0,ncel,nsurv)
min_wo_survey = matrix(0,ncel,nsurv)

for (m in 1:nsurv)
{
  n_wo = 0
  nmin_wo = 0
  for (i in 1:nresp)
  {
    if (!is.na(GRAND_NEUT_oplcat[i,m+5])&GRAND_NEUT_oplcat[i,m+5]>4)
    {
      db = dbinom(GRAND_NEUT_oplcat[i,m],GRAND_NEUT_oplcat[i,m+5],seq(0.005,0.995,0.01))
      uiti = matrix(db,ncel,1)/(sum(db)*stap)
      if (GRAND_NEUT_oplcat[i,11]==6) { uit_wo_survey[,m] = uit_wo_survey[,m] + uiti ; n_wo = n_wo + 1 }
      if (GRAND_NEUT_oplcat[i,11]!=6) { min_wo_survey[,m] = min_wo_survey[,m] + uiti ; nmin_wo = nmin_wo + 1 }
    }
  }
  uit_wo_survey[,m] = uit_wo_survey[,m] / n_wo;print(n_wo)
  min_wo_survey[,m] = min_wo_survey[,m] / nmin_wo;print(nmin_wo)
}

p=seq(0.005,0.995,stap)
plot(p,min_wo_survey[,3],col="red",main='Neutral Responding Education PE',type='l',ylim=c(0,8),axes=FALSE,lwd=2,
xlab='Probability of Neutral Responding',ylab='Plausibility')
axis(side=1,at=seq(0,1,0.1));axis(side=2,at=seq(0,8,2),las=1)
lines(p,uit_wo_survey[,3],col="blue",lwd=2)
legend('topright',bty='n',c('Academic','Other than academic'),col=c('blue','red'),lty=c(1,1))

y1=uit_wo_survey[,3];y2=min_wo_survey[,3]
DIF=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
dif=(x-1)*stap-(y-1)*stap
DIF[x,y]=dif
}}
DIF=sign(DIF)
DEN=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
den=y1[x]*y2[y]*stap^2
DEN[x,y]=den
}}
CD_real=sum(DIF*DEN)/sum(DEN);CD_real



#####################################

par(mfrow=c(1,2))

p=seq(0.005,0.995,stap)
plot(p,min_15_24_survey[,5],col="red",main=c(paste('8A. Answering Dont Know'),paste('Age: Politics'),
paste('Estimated Profile-Based Cliffs Delta = .28'),paste('Its 99% Confidence Interval = (.22, .34)')),
type='l',ylim=c(0,8),axes=FALSE,lwd=2,xlab='Probability of Answering Dont Know',ylab='Plausibility')
axis(side=1,at=seq(0,1,0.1));axis(side=2,at=seq(0,8,2),las=1)
lines(p,uit_15_24_survey[,5],col="blue",lwd=2)
legend('topright',bty='n',c('15-24 years (R=595)','Older than 24 years (R=5125)'),
col=c('blue','red'),lty=c(1,1))

p=seq(0.005,0.995,stap)
plot(p,min_wo_survey[,3],col="red",main=c(paste('8B. Neutral Responding'),paste('Education: Personality'),
paste('Estimated Profile-Based Cliffs Delta = -.16'),paste('Its 99% Confidence Interval = (-.23, -.09)')),
type='l',ylim=c(0,8),axes=FALSE,lwd=2,xlab='Probability of Neutral Responding',ylab='Plausibility')
axis(side=1,at=seq(0,1,0.1));axis(side=2,at=seq(0,8,2),las=1)
lines(p,uit_wo_survey[,3],col="blue",lwd=2)
legend('topright',bty='n',c('Academic education (R=434)','Other than academic education (R=4723)'),
col=c('blue','red'),lty=c(1,1))


